import TimeUtils from "../../../utils/TimeUtils";

const constants = require('../../../common/Constants.js');
const netWork_utils = require('../../../http/NetworkUtils.js');
const regexUtils = require('../../../utils/RegexUtils.js');
const util = require('../../../utils/UmfUtils.js');
import * as cacheUtils from '../../../model/CacheUtils';

import {$Toptips} from '../../../components/CompRouter';

const PayeeInfoPage = {

    /**
     * 初始化收款人信息页
     * @param basisPage
     */
    initPayeeInfoPage(basisPage) {
        let cacheManager = new cacheUtils.cacheManager();
        cacheManager.init("newPayeeInfo", constants.STORAGE_FUNC.PAYEE_INFO);
        let cacheStorageData = cacheManager.getFunc();
        cacheStorageData = cacheStorageData? cacheStorageData: {};
        //设置实时缓存
        basisPage.setData({
            cacheManager: cacheManager,
            cacheStorageData: cacheStorageData,
            deadDate: cacheStorageData['deadDate'],
            payeeInidcardValid: cacheStorageData['payeeInidcardValid']
        })
        if (!cacheStorageData['payeeInidcardValid']){
          this.setData({
            payeeInidcardValid:"2099-09-09"
          })
        }
    },
    /**
     * 证件有效期---2017.11.8旭阳新需求
     * radio取值：长期/截止日期
     */
radioValidChange: function (e) {
        if (e.detail.value == "2099-09-09") {
          this.data.cacheManager.cacheFunc({ deadDate: false, payeeInidcardValid: e.detail.value})
            this.setData({
                deadDate: false,
                payeeInidcardValid: e.detail.value,
                storeManIdentityValid: e.detail.value,
            })
        } else {
          this.data.cacheManager.cacheFunc({deadDate: true})
            this.setData({
                deadDate: true,
                payeeInidcardValid: "",
                storeManIdentityValid: "",
            })
        }
    },
    /**
     * 有效期滚动选择监听
     * @param e
     */
    onValidDateChange: function (e) {
        this.data.cacheManager.cacheFunc({payeeInidcardValid: e.detail.value});
        this.setData({
            payeeInidcardValid: e.detail.value,
        });
    },

    /**
     * 提交收款人信息
     */
    payeeInfoSubmit: function (e) {
        let formValues = e.detail.value;
        formValues.payeeIdentity = util.removeSpace(formValues.payeeIdentity);

        let errorMsg = this._payeeInfo_checkForm(formValues);
        if (errorMsg) {
            $Toptips.error({text: errorMsg});
        } else {
            this._postPayeeInfo(formValues);
        }
    },

    /**
     * 收款人表单验证
     * @param formValues
     * @returns {*}
     * @private
     */
    _payeeInfo_checkForm(formValues) {
        let values = [
            [formValues.payeeName, constants.RegexTypes.PAYEE_NAME],
            [formValues.payeeIdentity, constants.RegexTypes.ID_CARD],
            [formValues.payeeMobile, constants.RegexTypes.MOBILE]
        ];
        let errorMsg = regexUtils.regexTypesWithMsg(values);
        if (errorMsg) {
            return errorMsg;
        }

        if (!this.data.payeeInidcardValid) {
            return "请选择证件有效期";
        } else if (TimeUtils.isLaterThen(this.data.today, this.data.payeeInidcardValid)) {
            return "请选择正确的证件有效期";
        }
        return '';
    },

    //网络请求
    _postPayeeInfo(e) {
        let params = {
            payeeName: e.payeeName,
            payeeIdentity: e.payeeIdentity,
            payeeMobile: e.payeeMobile,
            payeeInidcardValid: this.data.payeeInidcardValid
        };
        netWork_utils.buildRequest(constants.Interfaces.FUNC_PAYEEINF, params, {
            doHttpSucess: (data) => {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    this.data.cacheManager.clearFunc();
                    this.stepTo(1, data);
                    return true;
                }

                return false;
            }
        }).send();
    },

};

export default PayeeInfoPage;
